package cn.fql.basic.util;

import cn.fql.basic.jwt.JwtUtils;
import cn.fql.basic.jwt.LoginData;
import cn.fql.basic.jwt.Payload;
import cn.fql.basic.jwt.RsaUtils;
import cn.fql.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 *
 * 工具类： 获取当前登录人的信息
 */
public class LoginContext {
    public static Logininfo getLogininfo(HttpServletRequest request) {
        //获取请求头里面的token
        String token = request.getHeader("token");
        if(token!= null ){
            try {
                //1.获取公钥
                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader()
                        .getResource("auth_rsa.pub").getFile());
                //2.使用工具类解密获取载荷payload
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                //没有报错  就登录
                return payload.getLoginData().getLogininfo(); //返回登录信息
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }
}
